package com.lys.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.commonutils.R;
import com.lys.eduservice.entity.EduCourse;
import com.lys.eduservice.entity.vo.CoursePublishVo;
import com.lys.eduservice.entity.vo.CourseQuery;
import com.lys.eduservice.entity.vo.SaveCourseVo;
import com.lys.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Lyyyys
 * @since 2021-04-30
 */
@Api(description="课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("保存课程")
    public R saveCourse(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody SaveCourseVo courseVo){
        String eduCourseId = eduCourseService.saveCourseInfo(courseVo);
        if(!StringUtils.isEmpty(eduCourseId)){
            return R.ok().data("eduCourseId",eduCourseId);
        }else {
            return R.error().message("课程保存失败");
        }
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getCoursePublishVo/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishVoByCourseId(id);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    @ApiOperation(value = "最终发布课程信息")
    @PostMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page, limit);

        eduCourseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        boolean flag = eduCourseService.removeCourseById(id);
        return flag == true ? R.ok() : R.error();
    }
}

